package com.sunzhe.train.web;

import com.ibeetl.admin.core.web.JsonResult;
import com.ibeetl.member.annotation.LoginRequired;
import com.ibeetl.member.entity.Member;
import com.ibeetl.member.service.MemberService;
import com.sunzhe.train.service.PlatformService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * 首页 接口
 */
@Controller
public class SpaceController{

    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/space";

    @Autowired
   	PlatformService platformService;
    @Autowired
    MemberService memberService;
    
    @LoginRequired
    @RequestMapping(MODEL + "")
   	public ModelAndView index() {
   		ModelAndView view = new ModelAndView(MODEL + "/index.html");
   		return view;
   	}
    
    @LoginRequired
    @RequestMapping(MODEL + "/{page}.do")
  	public ModelAndView page(HttpServletRequest request, @PathVariable("page") String page) {
    	System.out.println("访问" + page);
    	ModelAndView view = new ModelAndView(MODEL + "/" + page + ".html");
  		return view;
  	}
    @LoginRequired
    @RequestMapping(MODEL + "/{dir}/{page}.do")
  	public ModelAndView page1(HttpServletRequest request, @PathVariable("dir") String dir, @PathVariable("page") String page) {
  		
    	ModelAndView view = new ModelAndView(MODEL + "/" + dir + "/" + page + ".html");
  		return view;
  	}
    
    /**
     * 修改密码
     * @param member
     * @return
     */
    @LoginRequired
    @PostMapping(MODEL + "/member/modifyPwd.json")
    @ResponseBody
	public JsonResult<Integer> modifyPwd(String newpassword, String oldpassword, String repassword, HttpServletRequest request) {
    	Member currentMember = platformService.getCurrentMember();
    	currentMember = this.memberService.getMemberById(currentMember.getId());
    	String dbOldpassword = currentMember.getPassword();
    	if (StringUtils.equals(dbOldpassword, oldpassword) == false) {
    		return JsonResult.failMessage("原始密码不正确");
    	} else {
    		if (StringUtils.equals(newpassword, repassword) == false) {
    			return JsonResult.failMessage("两次输入密码不正确");
    		}
    		memberService.resetPassword(currentMember.getId(), newpassword);
    		return JsonResult.success();
    	}
	}
    
    /**
     * 修改会员信息
     * @param member
     * @return
     */
    @LoginRequired
    @PostMapping(MODEL + "/member/modifyMemberInfo.json")
    @ResponseBody
	public JsonResult<Integer> modifyMemberInfo(Member member, HttpServletRequest request) {
    	String userName = request.getParameter("userName");
    	
    	Member currentMember = platformService.getCurrentMember();
    	currentMember = this.memberService.getMemberById(currentMember.getId());
    	currentMember.setUserName(userName);
    	memberService.updateMember(currentMember);
    	
    	
    	return JsonResult.success(currentMember.getId());
	}
}
